<template>
  <div class="dev-panel">
    <el-col :span="24">
      <el-row>
        <el-link type="primary" @click="testCode()">查看代码</el-link>
        <el-link type="primary" @click="analystCode()">查看解析</el-link>
      </el-row>
      <el-row>
        <code-editor ref="editor"></code-editor>
      </el-row>
    </el-col>
  </div>
</template>

<script>
import CodeEditor from './CodeEditor.vue'
import { antlr, TablePrimary } from 'rhombic'

import antlr4 from 'antlr4/src/antlr4/index'
import HplsqlLexer from '@/antlr4/hive/hive-2.3.3/HplsqlLexer'
import HplsqlParser from '@/antlr4/hive/hive-2.3.3/HplsqlParser'
import HplsqlVisitor from '@/antlr4/hive/hive-2.3.3/HplsqlVisitor'

export default {
  components: { CodeEditor },
  name: 'dev-panel',
  data() {
    return {
      _parserTree: {}
    }
  },
  methods: {
    testCode() {
      this.$message({ message: this.$refs.editor.codes })
    },
    mountNode(_treeNode, visitorCursor) {
      _treeNode = {
        ruleIndex: visitorCursor.ruleIndex,
        children: [],
        childCnt: 0
      }

      if (visitorCursor.children.length > 0) {
        visitorCursor.children.forEach((childCursor) => {
          this.mountNode(treeNode, childCursor)
        })
      }

      parentNode.children[parentNode.childCnt + 1] = {
        rule: nodeObj.ruleIndex,
        children: [],
        childCnt: 0
      }
      parentNode.childCnt += 1
    },
    analystCode() {
      //=====以下是antlr的实现方式
      const sql_input = this.$refs.editor.codes
      const chars = new antlr4.InputStream(sql_input)
      const lexer = new HplsqlLexer(chars)
      const tokens = new antlr4.CommonTokenStream(lexer)
      const parser = new HplsqlParser(tokens)
      parser.buildParseTrees = true
      const tree = parser.program()
      console.log(tree)
      this._parserTree = { index: tree.ruleIndex, children: [], childCnt: 0 }

      //=====以下是rhombic的实现方式
      // try {
      //   const parsingOptions = {
      //     // if double quotes should quote identifiers:
      //     doubleQuotedIdentifier: true
      //   }
      //   const q = antlr.parse(this.$refs.editor.codes, parsingOptions)

      //   console.log(q.getUsedTables()) // [{ tableName: "abc" }]
      //   // const getTable = (table: TablePrimary) => {
      //   //   /* Logic to retrieve table & columns metadata */
      //   // }

      //   // Whether to use "mergedLeaves" or "tree" lineage type
      //   // const mergedLeaves = true
      //   // const lineageOptions = {
      //   //   positionalRefsEnabled: false
      //   // }
      //   // const lineage = q.getLineage(getTable, mergedLeaves, lineageOptions)
      // } catch (e) {
      //   // Parsing errors
      // }

      // this.$message({
      //   message: tree,
      //   type: 'success'
      // })
    }
  }
}
</script>

<style>
.dev-panel {
  flex: 1;
  display: flex;
  /* flex-direction: column; */
}
.el-link {
  margin-right: 10px;
}
</style>
